package controller

import (
	"sugar/container"
	"sugar/database"
	"sugar/middleware/cache"
)

// BaseOp ...定义了各种中间件，数据库操作使用情况
type BaseOp struct {
	CacheOp      cache.CacheOp
	DbOp         *database.DatabaseOp
	K8sOp        *container.KubernetesOperation
	useDB        bool
	useK8sClient bool
}

func NewBaseOp() *BaseOp {
	return &BaseOp{
		DbOp:         database.NewDatabaseOp(),
		CacheOp:      cache.NewCacheOp(),
		useDB:        true,
		useK8sClient: false,
	}
}

type BeginOptions func(*BaseOp)

func NotUseDBOption(c *BaseOp) {
	c.useDB = false
}

func UseK8sOption(c *BaseOp) {
	c.useK8sClient = true
}

func (c *BaseOp) BeginOp(opts ...BeginOptions) {
	for _, opt := range opts {
		opt(c)
	}
	if c.useDB {
		err := c.DbOp.Begin()
		if err != nil {
			panic(err)
		}
	}
	if c.useK8sClient {
		c.K8sOp = container.NewK8sOp()
	}
}

func (c *BaseOp) EndOp() {
	// fmt.Printf("db close!\n")
	err := recover()
	if err != nil {
		if c.useDB {
			c.DbOp.Rollback()
		}
		panic(err)
	} else {
		if c.useDB {
			c.DbOp.Commit()
		}
	}
}
